/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db.tasks;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Przemysław Bałdyga
 */
public class InitDbTask extends UpdateTask {

    private static final Logger log = Logger.getLogger("ZadanieInicjalizacjaBD");


    @Override
    public void execute() {
        try {

            //FIXME niekompatybilne przez IGNORE (chyba) / nie wiem czy to zadanie powinno tak dzialac
            Statement que;
            Connection c = getDS().getConnection();
            
            
            String st = "INSERT IGNORE INTO zad_polozenie VALUES(" + getIdFactory().generateId() + ",NULL)";
            que = c.createStatement();
            que.executeUpdate(st);
            que.close();
            st = "INSERT IGNORE INTO zad_reszta VALUES(" + getIdFactory().generateId() + ",NULL,5,5,NULL,NULL)";
            que = c.createStatement();
            que.executeUpdate(st);
            que.close();
            st = "INSERT IGNORE INTO zadania_id VALUES((SELECT id_polozenie FROM zad_polozenie where polozenie is NULL),(SELECT id_reszta FROM zad_reszta where cel is NULL)) ";
            que = c.createStatement();
            que.executeUpdate(st);
            que.close();
        } catch (SQLException ex) {
            log.log(Level.SEVERE, "Nie udało sie wybrać zadania z bazy danych", ex);
            //getIdFactory().fail();
        }
    }
}
